package com.im.dao;

import com.im.pojo.*;
import com.im.pojoClass.ImLatelyList;
import com.im.pojoClass.ImSendList;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;

@Repository
public interface UserDao {

    /**
     * 登陆
     **/
    @Select("SELECT user_id,user_name,user_img FROM im_user WHERE user_id = #{user_id} AND user_pass = #{user_pass}")
    public ImUser login(String user_id, String user_pass);

    /**
     * 查找用户
     **/
    @Select("SELECT user_id,user_name,user_img FROM im_user WHERE user_id = #{user_id}")
    public ImUser selectUser(String user_id);

    /**
     * 查找最近联系人表
     **/
    @Select("SELECT send_id,receive_id,message_id FROM im_lately WHERE send_id = #{send_id} and receive_id = #{receive_id}")
    public ImLately selectLately(String send_id, String receive_id);

    /**
     * 查找全部的最近联系人列表
     **/
    @Select("SELECT u.user_id,u.user_name,u.user_img,m.message_id,m.message_data,m.message_type,m.message_date,l.read_message FROM im_lately l LEFT JOIN im_message m ON l.message_id = m.message_id LEFT JOIN im_user u ON l.receive_id = u.user_id WHERE l.send_id = #{send_id}")
    @Results({
            @Result(column = "user_id", property = "imUser.user_id"),
            @Result(column = "user_name", property = "imUser.user_name"),
            @Result(column = "user_img", property = "imUser.user_img"),
            @Result(column = "message_id", property = "imMessage.message_id"),
            @Result(column = "message_data", property = "imMessage.message_data"),
            @Result(column = "message_type", property = "imMessage.message_type"),
            @Result(column = "message_date", property = "imMessage.message_date"),
            @Result(column = "read_message", property = "imLately.read_message"),
    })
    public ArrayList<ImLatelyList> selectLatelyList(String send_id);

    /**
     * 查找好友互发消息消息表
     **/
    @Select("SELECT m.message_id,m.message_type,m.message_date,m.message_data,s.receive_id,s.send_id FROM im_send s LEFT JOIN im_message m ON s.message_id = m.message_id WHERE (s.send_id = #{send_id} AND s.receive_id = #{receive_id}) OR (s.send_id = #{receive_id} AND s.receive_id = #{send_id}) ORDER BY message_date DESC LIMIT #{page},#{num};")
    @Results({
            @Result(column = "message_id", property = "imMessage.message_id"),
            @Result(column = "message_type", property = "imMessage.message_type"),
            @Result(column = "message_date", property = "imMessage.message_date"),
            @Result(column = "message_data", property = "imMessage.message_data"),
            @Result(column = "receive_id", property = "imSend.receive_id"),
            @Result(column = "send_id", property = "imSend.send_id"),
    })
    public ArrayList<ImSendList> selectSendList(String send_id, String receive_id, Integer page, Integer num);

    /**
     * 查找好友申请未同意列表
     **/
    @Select("SELECT u.user_id,u.user_name,u.user_img FROM im_apply a LEFT JOIN im_user u ON a.send_id = u.user_id where a.receive_id = #{receive_id} and a.agree = 0")
    public ArrayList<ImUser> selectUserApplyNotAgreeList(String receive_id);

    /**
     * 查找好友申请是否同意
     **/
    @Select("SELECT * from im_apply where send_id = #{send_id} and receive_id = #{receive_id} and agree = 1")
    public ImApply selectUserApplyYesAgree(String send_id, String receive_id);

    /**
     * 查找好友是否申请
     **/
    @Select("SELECT * from im_apply where send_id = #{send_id} and receive_id = #{receive_id}")
    public ImApply selectUserApplyAgree(String send_id, String receive_id);

    /**
     * 添加用户
     **/
    @Insert("INSERT INTO im_user(user_id,user_name,user_pass,user_img) VALUES (#{user_id},#{user_name},#{user_pass},#{user_img})")
    public int register(ImUser user);

    /**
     * 添加消息
     **/
    @Insert("INSERT INTO im_message(`message_id`,`message_data`,`message_type`,`message_date`) VALUES(#{message_id},#{message_data},#{message_type},#{message_date})")
    public Integer insertMessage(ImMessage imMessage);

    /**
     * 添加消息发送
     **/
    @Insert("INSERT INTO im_send(`send_id`,`receive_id`,`message_id`) VALUES(#{send_id},#{receive_id},#{message_id})")
    public Integer insertSend(ImSend imSend);

    /**
     * 添加最近联系人
     **/
    @Insert("INSERT INTO im_lately(`send_id`,`receive_id`,`message_id`,`read_message`) VALUES(#{send_id},#{receive_id},#{message_id},#{read_message})")
    public Integer insertLately(ImLately imLately);

    /**
     * 添加好友申请
     **/
    @Insert("INSERT INTO im_apply(`send_id`,`receive_id`,`agree`) VALUES(#{send_id},#{receive_id},#{agree})")
    public Integer insertApply(ImApply imApply);

    /**
     * 修改好友申请同意
     **/
    @Update("UPDATE im_apply SET agree = 1 WHERE send_id = #{send_id} and receive_id = #{receive_id}")
    public Integer updateApplyAgree(ImApply imApply);

    /**
     * 修改最近联系人: 消息id
     **/
    @Update("UPDATE im_lately SET message_id = #{message_id} WHERE send_id = #{send_id} and receive_id = #{receive_id}")
    public Integer updateLatelyMessage(ImLately imLately);

    /**
     * 修改最近联系人: 未读消息数增加N
     **/
    @Update("UPDATE im_lately SET read_message = read_message + #{read_message} WHERE send_id = #{send_id} and receive_id = #{receive_id}")
    public Integer updateLatelyReadN(ImLately imLately);

    /**
     * 修改最近联系人: 未读消息数
     **/
    @Update("UPDATE im_lately SET read_message = #{read_message} WHERE send_id = #{send_id} and receive_id = #{receive_id}")
    public Integer updateLatelyRead(ImLately imLately);

    /**
     * 清空所有数据表
     **/
    @Update("truncate table im_apply;truncate table im_lately;truncate table im_message;truncate table im_send;truncate table im_user;")
    public Integer truncate();

    @Select("SELECT * FROM test where a = 1")
    public Integer test();

}
